package com.jsgt.greekfire.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jsgt.greekfire.R;

import java.util.List;

public class MenuDialog extends BaseDialog {
	private ListView listView;
	private List<String> mDataList;
	private List<Integer> mIconList;
	private LayoutInflater mLayoutInflater;
	private OnItemClickListener onItemClickListener;
	private String title;
	private int mTitleGravity,mItemGravity;
	public MenuDialog(Context activity, List<String> menuList, String title) {
		this(activity,menuList,null,title,Gravity.LEFT|Gravity.CENTER_VERTICAL,Gravity.LEFT|Gravity.CENTER_VERTICAL);
	}
	public MenuDialog(Context activity, List<String> menuList, List<Integer> icons,String title,int itemGravity,int titleGravity) {
		super(activity, R.style.translucentDialog);
		mDataList = menuList;
		mIconList = icons;
		mLayoutInflater = LayoutInflater.from(activity);
		this.title = title;
		mItemGravity = itemGravity;
		mTitleGravity = titleGravity;

	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.dialog_menu);
		listView = (ListView) findViewById(R.id.listview_dialog_menu);
		TextView tvTitle = (TextView) findViewById(R.id.tv_dialog_title);
		if (!TextUtils.isEmpty(title)) {
			tvTitle.setText(title);
			findViewById(R.id.dialog_title_line).setVisibility(View.VISIBLE);
			tvTitle.setGravity(mTitleGravity);
		} else {
			tvTitle.setVisibility(View.GONE);
			findViewById(R.id.dialog_title_line).setVisibility(View.GONE);
		}

		listView.setAdapter(new DialogMenuAdapter());
		listView.setOnItemClickListener(onItemClickListener);
		findViewById(R.id.linear_outside).setOnTouchListener(
				new OnTouchListener() {

					@Override
					public boolean onTouch(View v, MotionEvent event) {
						if(_canceledOnTouchOutside)
							dismiss();
						return false;
					}
				});
	}
	public void setDataList(List<String> list){
		mDataList = list;
		listView.setAdapter(new DialogMenuAdapter());
	}
	public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
		this.onItemClickListener = onItemClickListener;
	}

	private class DialogMenuAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mDataList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView tv = (TextView) mLayoutInflater.inflate(
					R.layout.dialog_menu_item, null);
			tv.setText(mDataList.get(position));
			if(mIconList != null && mIconList.size()>position){
				tv.setCompoundDrawablesRelativeWithIntrinsicBounds(mIconList.get(position),0,0,0);
			}
			tv.setGravity(mItemGravity);
			return tv;
		}
	}
}
